package lu.med.praxiswahl.wahlumed.client.gravtest;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;


/**
 * 
 * @author Lars Holger Roth
 */
public class GravTestCellRenderer implements TableCellRenderer
{
    private Color black = new Color(0, 0, 190);
    private Color white = new Color(255, 255, 255);
    private Color red = new Color(235, 0, 0);
    private Font bold = new Font("Arial", Font.BOLD, 12);
    private Font extrabold = new Font("Arial", Font.BOLD, 14);
    private Color blue = new Color(0, 0, 250);
    private GravTestDataModel model;
    private String str;

    public GravTestCellRenderer(GravTestDataModel model)
    {
	this.model = model;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
	    boolean isSelected, boolean hasFocus, int row, int column)
    {
	JLabel label = new JLabel();

	label.setOpaque(true);
	Border b = BorderFactory.createEmptyBorder(1, 1, 1, 1);
	label.setBorder(b);
	label.setFont(table.getFont());
	label.setForeground(table.getForeground());
	label.setBackground(table.getBackground());

	// Sonderf�lle f�r die Farben
	// Jedes Datum wird Rot eingefärbt
	if (column == 1)
	{
	    str = (String) model.getValueAt(row, column);
	    if (!str.equals("     \""))
	    {
		label.setForeground(red);
		label.setBackground(white);
		label.setFont(bold);
	    }
	}

	if (model == null)
	{
	    label.setText("");
	}
	else
	{
	    label.setText((String) model.getValueAt(row, column));
	}

	if (hasFocus)
	{
	    label.setBackground(black);
	    label.setForeground(white);
	}
	else
	{
	    // Angezeigte Spalte in Modellspalte umwandeln
	    column = table.convertColumnIndexToModel(column);
	}

	if (column == 4)
	{
	    // Muss ein spezielles Panel zurückgeben
	    label.setHorizontalAlignment(SwingConstants.CENTER);
	    if (label.getText().equals("s.+"))
	    {
		JPanel gb = new JPanel(new GridLayout(0, 2));
		JLabel l1, l2;
		l1 = new JLabel(" schw.");
		l2 = new JLabel("+");
		l2.setForeground(red);
		gb.add(l1);
		// gb.add(l2);
		gb.setBackground(white);
		return gb;
	    }
	    else
		label.setFont(extrabold);
	    if (label.getText().equals("+"))
		label.setForeground(red);
	}

	if (isSelected)
	{
	    label.setBackground(blue);
	    label.setForeground(white);
	    label.setFont(bold);
	}

	return label;
    }
}